题目链接 :http://codeforces.com/gym/101466/problem/A
A. Gaby And Addition
time limit per test
6.0 s
memory limit per test
1024 MB
input
standard input
output
standard output
Gaby is a little baby who loves playing with numbers. Recently she has learned how to add 2 numbers using the standard addition algorithm which we summarize in 3 steps:
- Line up the numbers vertically matching digits places.
- Add together the numbers that share the same place value from right to left.
Carry if necessary. it means when adding two numbers we will get something like this:
Unfortunately as Gaby is too young she doesn’t know what the third step means so she just omitted this step using her own standard algorithm (Gaby’s addition algorithm). When adding two numbers without carrying when necessary she gets something like the following:
Gaby loves playing with numbers so she wants to practice the algorithm she has just learned (in the way she learned it) with a list of numbers adding every possible pair looking for the pair which generates the largest value and the smallest one.
She needs to check if she is doing it correctly so she asks for your help to find the largest and the smallest value generated from the list of numbers using Gaby’s addition algorithm.
Input
The input starts with an integer _n_ (2 ≤ _n_ ≤ 106) indicating the number of integers Gaby will be playing with. The next line contains _n_numbers _ni_ (0 ≤ _ni_ ≤ 1018) separated by a single space.
Output
Output the smallest and the largest number you can get from adding two numbers from the list using Gaby’s addition algorithm.
Examples
input
Copy
1 | 6 |
output
Copy
1 | 0 99 |
input
Copy
1 | 7 |
output
Copy
1 | 52990443860776502 972190360051424498 |
Note
In the first sample input this is how you get the minimum and the maximum value
题意:给 n个数求不进位加法,两个数和的最大值,最小值。
题解:分别对每个数字拆分成 18个位,每个位是 0-9 的数字,然后用每个位建一个字典树。
就形成了一棵以0结点为根节点,然后每层分配0-9 儿子节点的字典树,然后每次查询和当前值相加最大值和最小值,分别每次从取模10最大的和最小的节点匹配。
例如 当前位是 5 最大值直接从当前位儿子节点 4开始找 如果存在直接求两个数的和,否则继续3 2 1.。。。
1 | #include<iostream> |